/**
 *
 */
package com.ean.backpack.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ean.backpack.dto.SearchFormDTO;
import com.ean.backpack.other.DestinationFormatter;
import com.ean.backpack.security.UserContext;
import com.ean.backpack.service.ActivepropertylistService;
import com.google.gson.Gson;

/**
 * Main controller, handles the index page, autocompletion and the root pages.
 * 
 * @author valentin, cristian.daraban
 * 
 */
@Controller
public class HomeController extends AbstractController {

	@Autowired
	private UserContext userContext;

	@Autowired
	private ActivepropertylistService activeProp;

	private List<String> autocompleteList;

	/**
	 * Renders the index view it via freemarker * template.
	 * Contains test data.
	 * 
	 * @TODO Needs cleanup
	 * 
	 * @param model
	 * @return the index view (FTL)
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(@ModelAttribute("model") ModelMap model,
			@ModelAttribute("search") SearchFormDTO search) {

		logger.info(Boolean.toString(userContext.isUserLogged()));

		logger.info(search.toString());

		ArrayList<HashMap<String, String>> pictures = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> picture = new HashMap<String, String>();
		HashMap<String, String> picture2 = new HashMap<String, String>();
		picture.put("top", "100");
		picture.put("left", "100");
		picture.put("rot", "30");
		picture.put("image", "test.jpg");
		picture.put("desc", "O descriere descrieritoare !");
		picture.put("thumb", "test_thumb.jpg");
		pictures.add(picture);

		picture2.put("top", "150");
		picture2.put("left", "150");
		picture2.put("rot", "-10");
		picture2.put("image", "test2.jpg");
		picture2.put("desc", "O descriere descrieritoare !");
		picture2.put("thumb", "test2_thumb.jpg");
		pictures.add(picture2);

		model.put("isUserLoggedIn", userContext.isUserLogged());
		model.put("pictures", pictures);

		return "index";
	}

	/**
	 * Renders the protected view for security test
	 * 
	 * @return the protected view
	 */
	@RequestMapping(value = "/protected", method = RequestMethod.GET)
	public String index() {
		return "home/protected";
	}

	/**
	 * Redirects root to index
	 * 
	 * @return the index view
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String redirectToIndex() {
		return "redirect:/index";
	}

	/**
	 * Sends a list with the available destinations to autocomplete function
	 * on the frontend.
	 * 
	 * @param String - This represents a part of the destination or full destination
	 * 
	 * @return A json-like simple array structure
	 * 
	 * @author cristian.daraban
	 */
	@RequestMapping(value = "/autocomplete", method = RequestMethod.GET)
	public @ResponseBody
	String callAutocomplete(@RequestParam(value = "term") String term) {
		DestinationFormatter dest = new DestinationFormatter();
		dest.process(term, ",");

		if (dest.isValidDestination()) {
			autocompleteList = activeProp.findByCity(dest.getCity(), dest.getCountry(), dest.getState());
			if ((dest.getState() != null || dest.getState() != "") && dest.getCountry() != "") {
				autocompleteList.addAll(activeProp.findByCity(dest.getCity(), dest.getState(), dest.getCountry()));
			}
		}
		Gson gson = new Gson();

		return gson.toJson(autocompleteList.subList(0, 10));
	}

}
